﻿using System;
using System.Linq;
using System.ComponentModel;

namespace Inaction.DbSchame
{
    [Serializable]
    [DisplayName("列信息")]
    public class Column : NameObject
    {
        #region 属性
        [DisplayName("所属表")]
        public Table Table { get; set; }
        [DisplayName("是否为主键")]
        public bool IsPrimaryKey { get; set; }
        [DisplayName("数据类型")]
        public Type SystemType { get; set; }
        [DisplayName("数据库中的数据类型")]
        public string DbType { get; set; }
        [DisplayName("是否可为空")]
        public bool Nullable { get; set; }
        [DisplayName("表中的顺序号")]
        public int Ordinal { get; set; }
        [DisplayName("默认值")]
        public object DefaultValue { get; set; }
        [DisplayName("字符串(VARCHAR)或字符(CHAR)的数据长度")]
        public int? Length { get; set; }
        [DisplayName("精度")]
        public int? Precision { get; set; }
        [DisplayName("小数位数")]
        public int? Scale { get; set; }
        [DisplayName("是否为外键")]
        public bool IsForeignKey
        {
            get;
            internal set;
        }
        [DisplayName("是否为惟一索引的某个列")]
        public bool IsUniqueKey
        {
            get;
            internal set;
        }
        bool isEnum = false;
        public bool IsEnum
        {
            get { return isEnum; }
            internal set { isEnum = value; }
        }

        #endregion

        string equalsClause = string.Empty;
        [Browsable(false)]
        public string EqualsClause
        {
            get
            {
                if (string.IsNullOrEmpty(equalsClause))
                {
                    equalsClause = FormatName + " = " + ParameterName;
                }
                return equalsClause;
            }
        }

        string notEqualsClause = string.Empty;
        [Browsable(false)]
        public string NotEqualsClause
        {
            get
            {
                if (string.IsNullOrEmpty(notEqualsClause))
                {
                    notEqualsClause = FormatName + " <> " + ParameterName;
                }
                return notEqualsClause;
            }
        }

        string parameterName = string.Empty;
        [Browsable(false)]
        public string ParameterName
        {
            get
            {
                if (string.IsNullOrEmpty(parameterName))
                {
                    parameterName = DbProvider.Instance.ParaFix + Name;
                }
                return parameterName;
            }
        }

        public override string ToString()
        {
            return (Table == null ? "" : Table.Name + ":") + Name;
        }
    }
}
